package com.kaili.db.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.kaili.common.util.CommonUtil;
import com.kaili.db.mapper.HsOptQueryMapper;
import com.kaili.db.service.IHsOptQueryService;
import com.kaili.pojo.dto.query.QueryBean;
import com.kaili.pojo.entity.HsOptQuery;
import com.kaili.pojo.vo.QueryDetailVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cgm
 * @since 2018-05-08
 */
@Service
public class HsOptQueryServiceImpl extends ServiceImpl<HsOptQueryMapper, HsOptQuery> implements IHsOptQueryService {
    @Autowired
    private MessageSource messageSource;

    @Override
    public boolean insertBatch(List<HsOptQuery> list){
        baseMapper.insertBatch(list);
        return true;
    }

    @Override
    public List<QueryBean> getQueryInfo(String ewbNo) {
        Page<QueryBean> page = new Page<QueryBean>(1, 3);
        Map map = new HashMap();
        map.put("ewbNo", ewbNo);
        List<QueryBean> list = baseMapper.getQueryInfo(page, map);
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                QueryBean bean = list.get(i);
                Map search1 = new HashMap();
                search1.put("ewbNo", bean.getEwbNo());
                search1.put("status", 1);
                List<QueryDetailVo>  queryDetailVo =baseMapper.getQueryByEwbNo(search1);
                for(QueryDetailVo  querylist:queryDetailVo){
                    //获取是国际化,中,英文自动转换
                    String msg = CommonUtil.getMessage(messageSource, querylist.getStatus());
                    querylist.setStatus(msg);
                    bean.setDetail(queryDetailVo);
                }
            }
            return list;
        }
        return list;
    }

    public void deleteQueryInfo(String ewbNo, String optId) {
        Wrapper<HsOptQuery> wrapper = new EntityWrapper<>();
        wrapper.where("operation_id={0} and ewb_no={1}", optId, ewbNo);
        HsOptQuery optQuery = new HsOptQuery();
        optQuery.setStatus(0);
        baseMapper.update(optQuery, wrapper);
    }
}
